import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '../components/Login'
// import Home from '../components/Home'
// import Main from '../components/Main'
// import Users from '../components/user/users'
// import Rights from '../components/power/Rights'
// import Roles from '../components/power/Roles'
// import Categories from '../components/goods/Categories'
// import Params from '../components/goods/Params'
// import Goods from '../components/goods/Goods'
// import Add from '../components/goods/Add'
// import Orders from '../components/orders/Orders'
// import Reports from '../components/report/Report'

const Login = () =>
    import ( /* webpackChunkName:"login_home_welcome" */ '../components/Login.vue')

const Home = () =>
    import ( /* webpackChunkName:"login_home_welcome" */ '../components/Home.vue')

const Main = () =>
    import ( /* webpackChunkName:"login_home_welcome" */ '../components/Main.vue')

const Users = () =>
    import ( /* webpackChunkName:"user" */ '../components/user/users.vue')

const Rights = () =>
    import ( /* webpackChunkName:"power" */ '../components/power/Rights.vue')

const Roles = () =>
    import ( /* webpackChunkName:"power" */ '../components/power/Roles.vue')

const Categories = () =>
    import ( /* webpackChunkName:"goods" */ '../components/goods/Categories.vue')

const Params = () =>
    import ( /* webpackChunkName:"goods" */ '../components/goods/Params.vue')

const Goods = () =>
    import ( /* webpackChunkName:"goods" */ '../components/goods/Goods.vue')

const Add = () =>
    import ( /* webpackChunkName:"goods" */ '../components/goods/Add.vue')

const Orders = () =>
    import ( /* webpackChunkName:"order" */ '../components/orders/Orders.vue')

const Reports = () =>
    import ( /* webpackChunkName:"report" */ '../components/report/Report.vue')

Vue.use(VueRouter)

const routes = [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
        path: '/home',
        component: Home,
        redirect: '/welcome',
        children: [
            { path: '/welcome', component: Main },
            { path: '/users', component: Users },
            { path: '/rights', component: Rights },
            { path: '/roles', component: Roles },
            { path: '/categories', component: Categories },
            { path: '/params', component: Params },
            { path: '/goods', component: Goods },
            { path: '/goods/add', component: Add },
            { path: '/orders', component: Orders },
            { path: '/reports', component: Reports }
        ]
    }
]

const router = new VueRouter({
    routes
})
router.beforeEach((to, from, next) => {
    if (to.path == '/login') return next();
    const token = window.sessionStorage.getItem('token');
    if (token) {
        next()
    } else {
        return next('/login')
    }
})
export default router